<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>倒计时</title>
</head>

<body>

<form name="form1">
    <div style="margin: 0 auto">
      <center>离2016-11-17 14:21:42 还有:<br>
        <input type="textarea" name="time" size="35" style="text-align: center;font-size:18px;color:red;padding: 10px;margin-top:10px">
      </center>
    </div>
</form>
<script>
  const date = "2016-11-17 14:21:42";
  let timer = null;

  function getTime(date) {
    let temp;
    // 活动抽奖时间
    const arr = date.match(/\d+/g);
    const endYear = +arr[0];
    const endMonth = +arr[1];
    const endDay = +arr[2];
    const endHour = +arr[3];
    const endMinute = +arr[4];
    const endSecond = +arr[5];

    //当前时间
    let Today = new Date();
    const currentYear = Today.getFullYear();
    const currentMonth = Today.getMonth() + 1;
    const currentDay = Today.getDate();
    const currentHour = Today.getHours();
    const currentMinute = Today.getMinutes();
    const currentSecond = Today.getSeconds();
    Today = null;

    // 间隔时间
    let remainYear = endYear - currentYear;
    let remainMonth = endMonth - currentMonth;
    let remainDay = endDay - currentDay;
    let remainHour = endHour - currentHour;
    let remainMinute = endMinute - currentMinute;
    let remainSecond = endSecond - currentSecond;

    if (remainSecond < 0) {
      remainSecond = 60 + remainSecond;
      remainMinute = remainMinute - 1;
    }
    if (remainMinute < 0) {
      remainMinute = 60 + remainMinute;
      remainHour = remainHour - 1;
    }
    if (remainHour < 0) {
      remainHour = 24 + remainHour;
      remainDay = remainDay - 1;
    }
    if (remainDay < 0) {
      let day;
      const month = ( endMonth === currentMonth ) ? endMonth : endMonth - 1;
      if ([4,6,9,11].indexOf(month) !== -1) {
        day = 30;
      } else if (month === 2) {
        day = (endYear % 4 === 0) ? 29 : 28;
      } else {
        day = 31;
      }
      remainDay = day + remainDay;
      remainMonth = remainMonth - 1;
    }
    if (remainMonth < 0) {
      remainMonth = 12 + remainMonth;
      remainYear = remainYear - 1;
    }
    if (remainYear < 0) {
      temp = '时间已过';
    } else {
      if (remainYear <10) remainYear = `0${remainYear}`;
      if (remainMonth <10) remainMonth = `0${remainMonth}`;
      if (remainDay <10) remainDay = `0${remainDay}`;
      if (remainHour <10) remainHour = `0${remainHour}`;
      if (remainMinute <10) remainMinute = `0${remainMinute}`;
      if (remainSecond <10) remainSecond = `0${remainSecond}`;
      temp = remainYear + '年, ' + remainMonth + '月, ' + remainDay + '天, ' + remainHour + '小时, ' + remainMinute + '分, ' + remainSecond + '秒';
    }
    document.form1.time.value = temp;
    timer = setTimeout("getTime(date)",1000);
  }
  getTime(date);
</script>
</body>

</html>